package com.example.androidtest.view;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.viewpager.widget.ViewPager;

import com.example.androidtest.R;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class ViewPagerActivity extends AppCompatActivity {
    Toolbar tb;
    TabLayout tab;
    ViewPager viewPager;
    List<View> list;
    List<String> titles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jurisdiction1);

        tab = findViewById(R.id.tab);
        viewPager = findViewById(R.id.vp);
        tb = findViewById(R.id.tb);

        setSupportActionBar(tb);

        titles = new ArrayList<String>();
        setIngs(titles);

        list = new ArrayList<View>();
        View view1 = getLayoutInflater().inflate(R.layout.view1, null, false);
        View view2 = getLayoutInflater().inflate(R.layout.view2, null, false);
        View view3 = getLayoutInflater().inflate(R.layout.view3, null, false);
        list.add(view1);
        list.add(view2);
        list.add(view3);

        MyViewPagerAdapter myViewPagerAdapter = new MyViewPagerAdapter(list, titles);
        viewPager.setAdapter(myViewPagerAdapter);
        tab.setupWithViewPager(viewPager);


    }

    private void setIngs(List<String> titles) {
        for (int i = 1; i < 4; i++) {
            titles.add("图片" + i);
        }
        for (int i = 0; i < titles.size(); i++) {
            tab.addTab(tab.newTab().setText(titles.get(i)));
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
